import { Controller } from "../../mvc/core/Controller";
import { SBusiness } from "../../../Message/Public/SBusiness";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { DataEvent } from "../events/DataEvent";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { GameProxy } from "../mvc/GameProxy";
import { EPrictUnit } from "../../../Message/Public/EPrictUnit";
import { ItemData } from "../resource/info/ItemData";
import { ItemsUtil } from "../view/shortcuts/item/ItemsUtil";
import { SEntityId } from "../../../Message/Public/SEntityId";
import { TradeHintIcon } from "../view/trade/TradeHintIcon";
import { GModuleEvent } from "../mvc/GModuleEvent";
import { IView } from "../../mvc/interfaces/IView";
import { TradeModule } from "../../../modules/TradeModule";
import { WindowEvent } from "../../component/window/WindowEvent";
import { EBusinessOperation } from "../../../Message/Public/EBusinessOperation";
import { ErrorCode } from "../../common/error/ErrorCode";
import { SystemSetter } from "../view/system/SystemSetter";
import { UiHintIconView } from "../view/uiIconBtn/UiHintIconView";
import { Exception } from "../../../Framework/Util/Exception";
import { ITradeModule } from "../../../modules/interfaces/ITradeModule";
import { SPetInfo } from "../../../Message/Game/SPetInfo";
import { Vector } from "../../../__AS3__/vec/Vector";
type int = number;
//class TradeController
    
    export  class TradeController extends Controller
    {
        constructor()
        {
            
            super();this._requestList = new Vector.<SBusiness>();
            this._reqList = new Vector.<SBusiness>();
            Dispatcher.addEventListener(EventName.TradeRequest, this.onTradeRequest);
            Dispatcher.addEventListener(EventName.TradeOperationResult, this.onOperationResult);
            Dispatcher.addEventListener(EventName.TradeOperationError, this.onOperationError);
            Dispatcher.addEventListener(EventName.Trade_PutInItem, this.onPutInItem);
            NetDispatcher.addCmdListener(ServerCommand.TradeMsg, this.onTradeMsg);
            NetDispatcher.addCmdListener(ServerCommand.TradeApplySucess, this.onTradeApplySucess);
            NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.onMoneyAndGoodsChange);
            Dispatcher.addEventListener(EventName.TradeResetGold, this.onTradeResetGold);
            Dispatcher.addEventListener(EventName.TradeResetCoin, this.onTradeResetCoin);
            return;
        }

        /* internal  */onGoldInputChange(arg1: DataEvent): void
        {
            this._tradeModule.lock1 = false;
            this._tradeModule.lock2 = false;
            var loc1=Number(arg1.data);
            if (loc1 > cache.role.money.gold) 
            {
                MsgManager.showRollTipsMsg(Language.getString(41005));
                this._tradeModule.setGoldNum(cache.role.money.gold);
                GameProxy.tradeProxy.updateMoney(this.currentBusinessId, EPrictUnit._EPriceUnitGold, cache.role.money.gold);
            }
            else 
            {
                GameProxy.tradeProxy.updateMoney(this.currentBusinessId, EPrictUnit._EPriceUnitGold, loc1);
            }
            return;
        }

        /* internal  */onCoinInputChange(arg1: DataEvent): void
        {
            this._tradeModule.lock1 = false;
            this._tradeModule.lock2 = false;
            var loc1=Number(arg1.data);
            if (loc1 > cache.role.money.coin) 
            {
                MsgManager.showRollTipsMsg(Language.getString(41006));
                this._tradeModule.setCoinNum(cache.role.money.coin);
                GameProxy.tradeProxy.updateMoney(this.currentBusinessId, EPrictUnit._EPriceUnitCoin, cache.role.money.coin);
            }
            else 
            {
                GameProxy.tradeProxy.updateMoney(this.currentBusinessId, EPrictUnit._EPriceUnitCoin, Number(arg1.data));
            }
            return;
        }

        /* internal  */onPutInItem(arg1: DataEvent): void
        {
            var loc1=arg1.data as ItemData;
            if (ItemsUtil.isBind(loc1) || this._tradeModule.lock2) 
            {
                return;
            }
            this._tradeModule.putIn(arg1.data as ItemData);
            return;
        }

        public requestForTrade(arg1: SEntityId): void
        {
            GameProxy.tradeProxy.doApply(arg1);
            return;
        }

        public putInPet(arg1): void
        {
            if (this._tradeModule) 
            {
                this._tradeModule.putInPet(arg1.itemData);
            }
            return;
        }

        public isInTrade(): Boolean
        {
            return this._tradeModule && !this._tradeModule.isHide;
        }

        public get hintIcon(): TradeHintIcon
        {
            if (!this._tradeHintIcon) 
            {
                this._tradeHintIcon = new TradeHintIcon();
                this._tradeHintIcon.addEventListener(GModuleEvent.Event_AcceptTrade, this.acceptHandler);
                this._tradeHintIcon.addEventListener(GModuleEvent.Event_RejectTrade, this.rejectHandler);
            }
            return this._tradeHintIcon;
        }

        /* internal  */onTradeApplySucess(arg1: Object): void
        {
            MsgManager.showRollTipsMsg(Language.getString(41000));
            return;
        }

        protected /* override */ initView(): IView
        {
            if (!this._tradeModule) 
            {
                this._tradeModule = new TradeModule();
                this._tradeModule.addEventListener(GModuleEvent.Event_MoneyInputChange_gold, this.onGoldInputChange);
                this._tradeModule.addEventListener(GModuleEvent.Event_MoneyInputChange_coin, this.onCoinInputChange);
                this._tradeModule.addEventListener(GModuleEvent.Event_ItemChange, this.onChooseItemChange);
                this._tradeModule.addEventListener(GModuleEvent.Event_ConfirmTrade, this.onConfirmTrade);
                this._tradeModule.addEventListener(GModuleEvent.Event_LockTrade, this.onLockTrade);
                this._tradeModule.addEventListener(WindowEvent.CLOSE, this.closeTrade);
            }
            return this._tradeModule;
        }

        /* internal  */onTradeMsg(arg1: Object=null): void
        {
            var loc1=arg1 as SBusiness;
            var loc2=loc1.operation.__value;
            switch (loc2) 
            {
                case EBusinessOperation._EBusinessOperationUpdateItem:
                {
                    this._tradeModule.lock1 = false;
                    this._tradeModule.lock2 = false;
                    if (this.requestFromEntityId != cache.role.entityInfo.entityId.id) 
                    {
                        this._tradeModule.updateItem(loc1.toInfo.items);
                    }
                    else 
                    {
                        this._tradeModule.updateItem(loc1.fromInfo.items);
                    }
                    break;
                }
                case EBusinessOperation._EBusinessOperationUpdateMoney:
                {
                    this._tradeModule.lock1 = false;
                    this._tradeModule.lock2 = false;
                    if (this.requestFromEntityId != cache.role.entityInfo.entityId.id) 
                    {
                        this._tradeModule.updateMoney(loc1.toInfo.gold, loc1.toInfo.coin);
                    }
                    else 
                    {
                        this._tradeModule.updateMoney(loc1.fromInfo.gold, loc1.fromInfo.coin);
                    }
                    break;
                }
                case EBusinessOperation._EBusinessOperationLock:
                {
                    this._tradeModule.lock1 = true;
                    break;
                }
                case EBusinessOperation._EBusinessOperationConfirm:
                {
                    this.onTradeComfirm(loc1);
                    break;
                }
                case EBusinessOperation._EBusinessOperationCancel:
                {
                    this.onTradeCancel(loc1);
                    break;
                }
                case EBusinessOperation._EBusinessOperationEnd:
                {
                    this.onTradeEnd(loc1);
                    break;
                }
                case EBusinessOperation._EBusinessOperationAgree:
                {
                    this.onAgreeToTrade(loc1);
                    break;
                }
                case EBusinessOperation._EBusinessOperationApply:
                {
                    this.onTradeApply(loc1);
                    break;
                }
                case EBusinessOperation._EBusinessOperationError:
                {
                    this.onOpertionError(loc1);
                    break;
                }
                case EBusinessOperation._EBusinessOperationReject:
                {
                    this.onOperationReject(loc1);
                    break;
                }
            }
            return;
        }

        /* internal  */onMoneyAndGoodsChange(arg1: Object=null): void
        {
            if (!this._tradeModule) 
            {
            };
            return;
        }

        /* internal  */onTradeResetGold(arg1: DataEvent): void
        {
            var loc1=int(arg1.data);
            this._tradeModule.setGoldNum(loc1);
            return;
        }

        /* internal  */onTradeResetCoin(arg1: DataEvent): void
        {
            var loc1=int(arg1.data);
            this._tradeModule.setCoinNum(loc1);
            return;
        }

        /* internal  */onTradeComfirm(arg1: SBusiness): void
        {
            MsgManager.showRollTipsMsg(Language.getString(41001));
            return;
        }

        /* internal  */onTradeCancel(arg1: SBusiness): void
        {
            if (this._reqList) 
            {
                if (this._reqList.length > 0 && this.currentBusinessId == this._reqList[0].businessId) 
                {
                    this.currentBusinessId = null;
                }
            }
            this.view.hide();
            this.clearAtTradeEnd();
            MsgManager.showRollTipsMsg(Language.getString(41002));
            return;
        }

        /* internal  */onTradeEnd(arg1: SBusiness): void
        {
            if (this._reqList) 
            {
                if (this._reqList.length > 0 && this.currentBusinessId == this._reqList[0].businessId) 
                {
                    this._reqList.splice(0, 1);
                    this.currentBusinessId = null;
                }
            }
            else 
            {
                this.currentBusinessId = null;
            }
            this.view.hide();
            this.clearAtTradeEnd();
            MsgManager.showRollTipsMsg(Language.getString(41003));
            return;
        }

        /* internal  */onOperationReject(arg1: SBusiness): void
        {
            MsgManager.showRollTipsMsg(Language.getStringByParam(41004, arg1.fromInfo.name));
            return;
        }

        /* internal  */onOpertionError(arg1: SBusiness): void
        {
            MsgManager.showRollTipsMsg(ErrorCode.getErrorStringByCode(arg1.error.code));
            return;
        }

        /* internal  */onTradeApply(arg1: SBusiness): void
        {
            var loc2=0;
            if (SystemSetter.currentSetter.refuseTrade) 
            {
                GameProxy.tradeProxy.doOperation(EBusinessOperation._EBusinessOperationReject, arg1.businessId);
                return;
            }
            var loc1=false;
            if (this.hintIcon) 
            {
                this._reqList = this._tradeHintIcon._requestList;
                loc2 = 0;
                while (loc2 < this._reqList.length) 
                {
                    if ((this._reqList[loc2] as SBusiness).fromInfo.entityId.id == arg1.fromInfo.entityId.id) 
                    {
                        loc1 = true;
                        break;
                    }
                    ++loc2;
                }
                if (!loc1) 
                {
                    this._reqList.push(arg1);
                }
                this.showHint();
            }
            return;
        }

        /* internal  */showHint(): void
        {
            if (this._reqList.length > 0) 
            {
                UiHintIconView.instance.addHintIcon(this._tradeHintIcon);
                this.hintIcon.showHint();
                this.currentBusinessId = this._reqList[0].businessId;
            }
            else 
            {
                this.currentBusinessId = null;
            }
            return;
        }

        /* internal  */acceptHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as SBusiness;
            if (!loc1) 
            {
                return;
            }
            GameProxy.tradeProxy.doOperation(EBusinessOperation._EBusinessOperationAgree, loc1.businessId);
            return;
        }

        /* internal  */rejectHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as SBusiness;
            GameProxy.tradeProxy.doOperation(EBusinessOperation._EBusinessOperationReject, loc1.businessId);
            return;
        }

        /* internal  */clearAtTradeEnd(): void
        {
            cache.trade.isTrading = false;
            cache.trade.usedItems = new Map<any, any> /* flash.utils.Dictionary */();
            Dispatcher.dispatchEvent(new DataEvent(EventName.Trade_StatusChange));
            return;
        }

        /* internal  */onTradeRequest(arg1: DataEvent): void
        {
            this.requestForTrade(arg1.data as SEntityId);
            return;
        }

        /* internal  */onOperationResult(arg1: Object): void
        {
            var loc1=arg1.data.oper as EBusinessOperation;
            var loc2=loc1.value();
            switch (loc2) 
            {
                case EBusinessOperation._EBusinessOperationCancel:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.Trade_CancelSuccess));
                    break;
                }
            }
            return;
        }

        /* internal  */onOperationError(arg1: DataEvent): void
        {
            var loc1=arg1.data.oper as EBusinessOperation;
            var loc2=arg1.data.msg as Exception;
            var loc3=loc1.value();
            switch (loc3) 
            {
                case EBusinessOperation._EBusinessOperationConfirm:
                {
                    MsgManager.showRollTipsMsg(ErrorCode.getErrorStringByCode(loc2.code));
                    this._tradeModule.onComfirmError();
                    break;
                }
            }
            return;
        }

        /* internal  */closeTrade(arg1: WindowEvent): void
        {
            if (this.currentBusinessId != null) 
            {
                GameProxy.tradeProxy.doOperation(EBusinessOperation._EBusinessOperationCancel, this.currentBusinessId);
                this.clearAtTradeEnd();
            }
            (view as ITradeModule).reset();
            return;
        }

        /* internal  */onAgreeToTrade(arg1: SBusiness): void
        {
            var loc1=null;
            this.initView();
            this.requestFromEntityId = arg1.fromEntity.id;
            this.currentBusinessId = arg1.businessId;
            if (arg1.fromInfo.entityId.id != cache.role.entityInfo.entityId.id) 
            {
                loc1 = arg1.fromInfo.entityId;
            }
            else 
            {
                loc1 = arg1.toInfo.entityId;
            }
            if (this.requestFromEntityId != cache.role.entityInfo.entityId.id) 
            {
                this._tradeModule.startTrade(cache.role.playerInfo.name, cache.role.entityInfo.level, arg1.toInfo.name, arg1.toInfo.level, loc1);
            }
            else 
            {
                this._tradeModule.startTrade(cache.role.playerInfo.name, cache.role.entityInfo.level, arg1.fromInfo.name, arg1.fromInfo.level, loc1);
            }
            cache.trade.isTrading = true;
            Dispatcher.dispatchEvent(new DataEvent(EventName.Trade_StatusChange));
            return;
        }

        /* internal  */onConfirmTrade(arg1: DataEvent): void
        {
            GameProxy.tradeProxy.doOperation(EBusinessOperation._EBusinessOperationConfirm, this.currentBusinessId);
            return;
        }

        /* internal  */onLockTrade(arg1: DataEvent): void
        {
            GameProxy.tradeProxy.doOperation(EBusinessOperation._EBusinessOperationLock, this.currentBusinessId);
            return;
        }

        /* internal  */onChooseItemChange(arg1: DataEvent): void
        {
            this._tradeModule.lock1 = false;
            this._tradeModule.lock2 = false;
            var loc1="";
            if (arg1.data.itemData is SPetInfo || arg1.data.itemData is ItemData) 
            {
                loc1 = arg1.data.itemData.uid;
                cache.trade.usedItems[loc1] = arg1.data.amount;
            }
            else 
            {
                loc1 = arg1.data.itemData.itemData.uid;
                cache.trade.usedItems[loc1] = arg1.data.amount;
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.Trade_StatusChange));
            GameProxy.tradeProxy.updateItem(this.currentBusinessId, loc1, arg1.data.amount);
            return;
        }

        private /* var */_tradeModule: ITradeModule;

        private /* var */_tradeHintIcon: TradeHintIcon;

        private /* var */_requestList: Vector.<SBusiness>;

        private /* var */requestFromEntityId: int;

        public /* var */currentBusinessId: string="";

        private /* var */_reqList: Vector.<SBusiness>;
    }
